import { Service } from "./service";
import { config } from "config";



let { protocol } = location;
export class MemberService extends Service {
    private authUrl(path: string) {
        let url = `${protocol}//${config.serviceHost}/auth/${path}`;
        return url;
    }
    private url(path: string) {
        let url = `${config.memberUrl}${path}`;
        return url;
    }
    // login(username: string, password: string) {
    //     password = md5(password);
    //     let url = this.authUrl('user/login');
    //     return this.postByJson<LoginResult>(url, { username, password })
    //         .then(d => {
    //             Service.token.value = d.token;
    //         });
    // }
    // async register(mobile: string, password: string, smsId: string, verifyCode: string) {
    //     let url = `user/register`;
    //     let result = await this.postByJson<{ token: string, userId: string, appToken: string }>(url, {
    //         mobile, password, smsId, verifyCode
    //     })
    //     Service.token.value = result.token;
    //     return result;
    // }
    // async registerById(sellerId: string) {
    //     console.assert(sellerId != null);
    //     let url = `${Service.config.memberUrl}Seller/RegisterById`;
    //     let result = await this.postByJson<LoginResult>(url, { sellerId });
    //     Service.token.value = result.token;
    //     return result;
    // }
    // async resetPassword(mobile, password, smsId, verifyCode) {
    //     let url = `user/resetPassword`;
    //     let result = await this.postByJson<{ token: string, userId: string, appToken: string }>(url, { mobile, password, smsId, verifyCode })
    //     Service.token.value = result.token;
    //     return result;
    // }
    isMobileRegister(mobile: string) {
        let url = `user/isMobileRegister`;
        return this.get<boolean>(url, { mobile });
    }
    sendVerifyCode(mobile: string, type: 'register' | 'resetPassword') {
        let url = `sms/sendVerifyCode`;
        return this.postByJson<{ smsId: string }>(url, { mobile, type });
    }


    //============================================================
    // 店铺
    addStore(app: Store) {
        let url = this.authUrl('application/add')
        return this.postByJson(url, { name: app.Name }).then(data => {
            Object.assign(app, data);
            return data;
        });
    }
    updateStore(app: Store) {
        let url = this.authUrl('application/update')
        return this.putByJson(url, app);
    }
    deleteStore(app: Store) {
        console.assert(app != null)
        let url = this.authUrl('application/remove')
        return this.deleteByJson(url, { id: app.Id });
    }

    async saveStore(store: Store) {
        // let url = `${Service.config.memberUrl}Seller/UpdateApplication`
        // return this.postByJson(url, { app: store });
        // return this.updateStore(store)

        let url = this.url("Application/Save")
        let r = await this.postByJson<{ id: string }>(url, { app: store });
        Object.assign(store, r);
        return r
    }
    /**
     * 获取店铺
     */
    async getStore() {
        // if (!storeId) throw errors.argumentNull('storeId')
        console.assert(this.applicationId != null);
        let url = this.url("Application/Item")
        let r = await this.get<Store>(url, { id: this.applicationId });
        return r
    }
    async stores() {
        let url = this.url("Application/List")
        let r = await this.get<Store[]>(url);
        return r
    }
    // stores(): Promise<Array<Store>> {
    //     let url = this.url('Store/List')
    //     return this.get<Store[]>(url);
    // }
    //============================================================
    // async members(args: DataSourceSelectArguments) {

    //     let url = `${config.memberUrl}Member/List`;
    //     let users = await this.getByJson<DataSourceSelectResult<UserInfo>>(url, args);
    //     let userIds = users.dataItems.map(o => o.Id)
    //     let url1 = `${config.accountUrl}Account/GetAccountBalances`;
    //     let accounts = await this.getByJson<Account[]>(url1, { userIds });
    //     for (let i = 0; i < users.dataItems.length; i++) {
    //         let account = accounts[i];
    //         if (account == null)
    //             continue;

    //         users.dataItems[i].Balance = account.Balance;
    //     }

    //     return users;
    // }
    // me(): Promise<Seller> {
    //     // let url = `${Service.config.memberUrl}Seller/Me`;
    //     // let url1 = `${Service.config.weixinUrl}Member/GetSeller`;
    //     let url = 'auth/user/me'
    //     return Promise.all([this.get<Seller>(url), this.get<{ OpenId }>(url)]).then(args => {
    //         args[1] = args[1] || {} as any;
    //         args[0].openid = args[1].OpenId;
    //         return args[0];
    //     })
    // }
    // async roles() {
    //     let url = `${Service.config.memberUrl}Member/GroupList`;
    //     return this.get<Role[]>(url);
    // }
    // async saveRole(role: Role) {
    //     let url = `${Service.config.memberUrl}Member/SaveGroup`;
    //     // let role = { Name: name, Remark: remark } as Role
    //     return this.postByJson<Role[]>(url, { role });
    // }
    // async deleteRole(id: string) {
    //     let url = `${Service.config.memberUrl}Member/DeleteGroup`;
    //     return this.deleteByJson<Role[]>(url, { id });
    // }
    // weixinBind(openId: string) {
    //     let url = `${Service.config.memberUrl}Seller/Bind`;
    //     return this.put(url, { openId });
    // }
    // weixinUnbind(openId: string) {
    //     let url = `${Service.config.memberUrl}Seller/Unbind`;
    //     return this.put(url, { openId });
    // }
}

// export default new UserService();